package corp.sunny.commons.util.security;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.jasypt.util.text.BasicTextEncryptor;

/**
 * @File:JAASSerializer.java
 * @Date:Jun 15, 2011
 * @Author:Subhash Bavi
 * 
 * @Description: Serialize/Deserializer the session object (JAAS Principal)
 * @Copyright Sunny Corporation - 2011
 */
public class JAASSerializer {

	private static BasicTextEncryptor encryptor;

	static {
		encryptor = new BasicTextEncryptor();
		encryptor.setPassword("DONOTATTEMPTTOBREAKTHISCODE");
	}

	/**
	 * Serialize the object, encrypts and returns the String
	 * 
	 * @param obj
	 * @return
	 */
	public static String getSerializedObject(Object obj)
			throws JAASSerializerException {
		String serializedObj = null;

		ByteArrayOutputStream byteArrayOutputStream = null;
		ObjectOutputStream objectOutputStream = null;
		try {
			byteArrayOutputStream = new ByteArrayOutputStream();
			objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
			objectOutputStream.writeObject(obj);
			objectOutputStream.close();

			serializedObj = encryptor.encrypt(new String(byteArrayOutputStream
					.toByteArray()));
		} catch (IOException e) {
			throw new JAASSerializerException(e.getMessage());
		} finally {
			if (null != byteArrayOutputStream) {
				try {
					byteArrayOutputStream.close();
				} catch (IOException e) {
					throw new JAASSerializerException(e.getMessage());
				}
			}

			if (null != objectOutputStream) {
				try {
					objectOutputStream.close();
				} catch (IOException e) {
					throw new JAASSerializerException(e.getMessage());
				}
			}
		}
		return serializedObj;
	}

	/**
	 * Deserializes the object
	 * 
	 * @param serializedObj
	 * @return
	 * @throws JAASSerializerException
	 */
	public static Object getDeSerializedObject(String serializedObj)
			throws JAASSerializerException {

		Object obj = null;
		String plainText = null;

		ByteArrayInputStream byteArrayInputStream = null;
		ObjectInputStream objectInputStream = null;

		try {
			plainText = encryptor.decrypt(serializedObj);
			byteArrayInputStream = new ByteArrayInputStream(
					plainText.getBytes());
			objectInputStream = new ObjectInputStream(byteArrayInputStream);
			obj = objectInputStream.readObject();
			objectInputStream.close();
		} catch (IOException e) {
			throw new JAASSerializerException(e.getMessage());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (null != byteArrayInputStream) {
				try {
					byteArrayInputStream.close();
				} catch (IOException e) {
					throw new JAASSerializerException(e.getMessage());
				}
			}

			if (null != objectInputStream) {
				try {
					objectInputStream.close();
				} catch (IOException e) {
					throw new JAASSerializerException(e.getMessage());
				}
			}
		}
		return obj;
	}
}
